package com.example.sql.vo;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference;

public class UpdateJoinTable {

    private OracleSelectJoin firstJoin;
    private OracleSelectQueryBlock from;


    public UpdateJoinTable(OracleSelectJoin firstJoin, OracleSelectQueryBlock from) {
        this.firstJoin = firstJoin;
        this.from = from;
    }

    public OracleSelectJoin getJoinT5() {
        SQLExprTableSource leftTableName = new SQLExprTableSource("t2");
        SQLExprTableSource rightTableName = new SQLExprTableSource("t5");
        SQLExpr onCondition = SQLUtils.toSQLExpr(" t5.link_key = t2.link_key ");
        OracleSelectJoin replaceTable = new OracleSelectJoin(leftTableName, JoinType.INNER_JOIN, rightTableName, onCondition);
        return replaceTable;
    }

    public void doUpdate() {

        // 1 增加新的顶级节点
        // 2 设置条件
        // 3 把之前的设置为左孩子
        // 4 设置新节点的父级
        // 5 设置父级的from新节点

        OracleSelectJoin joinT5 = getJoinT5();
        joinT5.setLeft(this.firstJoin);
        joinT5.setParent(from);
        from.setFrom(joinT5);

    }
}
